//导航栏

let  burger = document.querySelector('.burger');
// let navMenu = document.querySelector('.nav-menu');
// let navMenuItems = document.querySelectorAll(".nav-menu li");
burger.addEventListener('click',function () {
    burger.classList.toggle('active');
//     // navMenu.classList.toggle('open');
//
//
//     // navMenuItems.forEach((item, index), function () {
//     //     if(item.style.animation){
//     //         item.style.animation = '';
//     //     }else{
//     //         item.style.animation = `0.3s ease-in slidenIn forwards ${index * 0.1 + 0.3}s`
//     //     }
//     //     }
//     // )
//
});
// 轮播
let items = document.getElementsByClassName('item');
let points= document.getElementsByClassName('point');
let goPreBtn = document.getElementById('goPre');
let goNextBtn = document.getElementById('goNext');
let index = 0;
let timer = 0
let clickedDate = null

/** 监听点击事件*/

goNextBtn.addEventListener('click',function () {
    clickedDate = new Date()
    goNext(true);
})
goPreBtn.addEventListener('click' ,function(){
    clickedDate = new Date()
    goNext(false);
})

for(var i=0 ; i < points.length; i++) {
    points[i].addEventListener('click',function (){
        clickedDate = new Date()
        var pointIndex = this.getAttribute('data-index');
        index = Number(pointIndex);
        goIndex();
    })
}

function cleanActive() {
    for (var i=0; i < items.length ; i++){
        items[i].className = 'item';
    }
    for (var i=0; i < points.length ; i++){
        points[i].className = 'point';
    }
}
function goIndex() {
    cleanActive();
    items[index].className = 'item active';
    points[index].className= 'point active';
}
function goNext(isNext) {
    if (isNext) {
        if(index < items.length - 1){
            index += 1;
        }else {
            index = 0;
        }
    }else {
        if(index == 0){
            index = items.length - 1;
        }else {
            index -= 1;
        }
    }
    goIndex();
}

/** timer */

function timerHandle() {
    if (clickedDate == null || (clickedDate != null && (new Date() - clickedDate > 2000))) {
        goNext(true);
    }
}

function startTimer() {
    console.log('==> 计时器开始')
    clearInterval(timer)
    timer = setInterval(timerHandle, 3000)
}

startTimer();
